package com.wider.foundation.nxfile;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NxFiles implements Iterator,Iterable{
    private NxFS        filesys;
    private NxFile      rootnode;
    private File[]      thefiles;
    private List        filelist;
    private Iterator    fileitrt;
    private NxFile      tempfile;



    /**
     * Constructor for constructing NxFile collection under root directory
     * @param filesys   The underlying NxFS
     * @param files     The list of files in Java File class
     */
    public NxFiles(NxFS filesystem, File[] files){
        filelist    = new ArrayList<NxFile>();
        filesys     = filesystem;
        thefiles    = files;
        rootnode    = null;

    }

    /**
     * Constructor for constructing NxFile collection under the node denoted by the NxFile file
     * @param file      The Nx abstract path of the directory which carries all the listed files
     * @param files     Array of the listed files under file.
     */
    public NxFiles(NxFile file, File[] files){
        filelist    = new ArrayList<NxFile>();
        filesys     = file.getFS();
        thefiles    = files;
        rootnode    = file;

    }

    @Override
    public boolean hasNext() {
        if (filelist.size() < thefiles.length){
            tempfile = filesys.createNxFile(thefiles[filelist.size()]);
            return true;
        }else{
            return fileitrt.hasNext();
        }
    }

    @Override
    public Object next() {
        if (filelist.size() < thefiles.length){
            filelist.add(tempfile);
            return tempfile;
        }else{
            return fileitrt.next();
        }
    }

    @Override
    public Iterator iterator() {
        fileitrt = filelist.iterator();
        return this;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("NxFiles DOESN'T support Remove operation on the NxFile collection.");
    }

}
